/*
 * @Description:
 * @Version: 1.0
 * @Autor: lzc
 * @Date: 2021-07-28 16:02:38
 * @LastEditors: lzc
 * @LastEditTime: 2021-11-23 14:58:11
 */
#include "store.h"

// char Test_Num_Link[RECORD_FAT_SIZE] = {10, 2, 3, 9, 11, 4, 5, 6, 7, 8, 12, 13, 14, 15, 16, 17, 1, 18, 0xff, 19}; // 20
char Test_Num_Link[RECORD_FAT_SIZE] = {1, 2, 3, 4, 0xff}; // 5
char Store_Num_Link_Info[RECORD_FAT_SIZE];
static char Empty_History_Flag = 0;

static at24cxx_device_t dev = RT_NULL;
static Store_Test_Status_t Store_Test_Status;
static Store_Test_Status_t Store_Test_Status_Read;
static Store_Test_Status_t Store_Test_Status_Write;
static Store_Test_Status_t Store_Test_Status_Delate;

// 重新初始化
extern void at24cxx_deinit(at24cxx_device_t dev);

/**
 * @function: EEPROM_Write_StructData
 * @brief: EEPROM 写结构体
 * @param {uint32_t} Write_Addr
 * @param {void} *data
 * @param {uint32_t} Write_Struct_Size
 * @return {*}
 * @author: lzc
 */
void EEPROM_Write_StructData(uint32_t Write_Addr, void *data, uint32_t Write_Struct_Size)
{
    uint8_t i = 0;
    uint8_t temp[1] = { 0 };
    dev = at24cxx_init(IIC_DEVICE_NAME, 0);
    for (i = 0; i < Write_Struct_Size; i++)
    {
        /*结构体中的成员类型不是一定的，因此，通过内存访问的形式，将结构体中数据按照
        字节依次取出，再写入flash。注意，使用结构体指针时，如果语句为：结构体首地址+1
        则地址会增加整个结构体的长度。理论上不能使用地址增加的形式访问结构体成员。
        如果需要特殊应用，则如下程序所示，先将结构体指针转换为uint8型指针，再通过
        地址增加的形式，就可以每次增加一个字节的地址了。*/
        memcpy(temp, ((uint8_t *)(data)) + i, 1); //从结构体所在内存中拷贝一个byte
        at24cxx_write(dev, Write_Addr + i, temp, 1);
        //rt_kprintf("WR DATA is %d\r\n", temp[0]);
    }
    rt_thread_mdelay(2);
    at24cxx_deinit(dev);
}

/**
 * @function: EEPROM_Read_StructData
 * @brief: EEPROM 读结构体
 * @param {uint32_t} Read_Addr
 * @param {void} *data
 * @param {uint32_t} Read_Struct_Size
 * @return {*}
 * @author: lzc
 */
void EEPROM_Read_StructData(uint32_t Read_Addr, void *data, uint32_t Read_Struct_Size)
{
    uint8_t i = 0;
    uint8_t temp[1] = { 0 };
    dev = at24cxx_init(IIC_DEVICE_NAME, 0);
    for (i = 0; i < Read_Struct_Size; i++)
    {
        /*结构体中的成员类型不是一定的，因此，通过内存访问的形式，将结构体中数据按照
        字节依次取出，再写入flash。注意，使用结构体指针时，如果语句为：结构体首地址+1
        则地址会增加整个结构体的长度。理论上不能使用地址增加的形式访问结构体成员。
        如果需要特殊应用，则如下程序所示，先将结构体指针转换为uint8型指针，再通过
        地址增加的形式，就可以每次增加一个字节的地址了。*/
        at24cxx_read(dev, Read_Addr + i, temp, 1);
        memcpy(((uint8_t *)(data)) + i, temp, 1); //从结构体所在内存中拷贝一个byte
        //rt_kprintf("RD DATA %d\r\n", temp[0]);
    }
    rt_thread_mdelay(2);
    at24cxx_deinit(dev);
}

/**
 * @function:Check_History_Empty_Status
 * @brief:
 * @param {*}
 * @return {*}
 * @author: lzc
 */
void Check_History_Empty_Status(void)
{
    int Count = 0;
    EEPROM_Read_StructData(RECORD_FAT_ADDR_START, Store_Num_Link_Info, sizeof(Store_Num_Link_Info));
    for (int i = 0; i < RECORD_FAT_SIZE; i++)
    {
        if (Store_Num_Link_Info[i] == DEFAULT_FF)
            ++Count;
        if (Count == RECORD_FAT_SIZE)
            Empty_History_Flag = 1;
    }
}
/**
 * @function: initLink
 * @brief:初始化
 * @param {*}
 * @return {*}
 * @author: lzc
 */
link *initLink()
{
    link *p = (link *)malloc(sizeof(link));//创建一个头结点
    link *temp = p; //声明一个指针指向头结点，用于遍历链表
    Check_History_Empty_Status();
    //生成链表
    for (int i = 0; i < RECORD_FAT_SIZE; i++)
    {
        link *a = (link *)malloc(sizeof(link));
        a->elem = Store_Num_Link_Info[i];
        a->Num_list = i + 1;
        a->next = NULL;
        temp->next = a;
        temp = temp->next;
    }
    return p;
}

/**
 * @function: initLink
 * @brief:初始化
 * @param {*}
 * @return {*}
 * @author: lzc
 */
link *initTestLink()
{
    link *p = (link *)malloc(sizeof(link));//创建一个头结点
    link *temp = p; //声明一个指针指向头结点，用于遍历链表
    //生成链表
    for (int i = 0; i < RECORD_FAT_SIZE; i++)
    {
        link *a = (link *)malloc(sizeof(link));
        a->elem = Test_Num_Link[i];
        a->Num_list = i + 1;
        a->next = NULL;
        temp->next = a;
        temp = temp->next;
    }
    return p;
}


/**
 * @function:
 * @brief:
 * @param {link} *p
 * @return {*}
 * @author: lzc
 */
void display(link *p)
{
    link *temp = p;//将temp指针重新指向头结点
    //只要temp指针指向的结点的next不是Null，就执行输出语句。
    while (temp->next)
    {
        temp = temp->next;
        rt_kprintf("%d ", temp->elem);
    }
    rt_kprintf("\n");
}

/**
 * @function: GetMax
 * @brief: 遍历链表求取最大值
 * @param {link} *L
 * @return {*}
 * @author: lzc
 */
int GetMax(link *L)
{
    if (L->next == NULL)
        return NULL;

    link *pmax, *p;
    pmax = L->next;     // 假定第一个结点中数据具有最大值
    p = L->next->next;

    while (p)
    {
        // 如果下一个结点存在
        // 如果p的值大于pmax的值，则重新赋值
        if (p->elem > pmax->elem
                && p->elem != DEFAULT_FF
                && pmax->elem != DEFAULT_FF)
            pmax = p;
        // 如果头一个就是0XFF,右挪
        else if ( pmax->elem == DEFAULT_FF )
        {
            pmax = p;
        }
        p = p->next;                 // 遍历链表
    }
    return pmax->elem;
}

/**
 * @function:selectElem_FindElem_ReturnPos
 * @brief:查(返回数据的位置，需要已知参数也就是需要知道序号)
 * @param {link} *p
 * @param {int} elem
 * @return {*}
 * @author: lzc
 */
int selectElem_FindElem_ReturnPos(link *p, int elem)
{
    link *t = p;
    int i = 1;
    while (t->next)
    {
        t = t->next;
        if (t->elem == elem)
        {
            return i;
        }
        i++;
    }
    return -1;
}

/**
 * @function:selectElem_FindElem_ReturnPos
 * @brief:查(返回数据的参数，需要已知位置也就是EEPROM的存储的位置)
 * @param {link} *p
 * @param {int} elem
 * @return {*}
 * @author: lzc
 */
int selectElem_FindPosition(link *p, int Pos)
{
    link *t = p;
    for (int i = 0; i < Pos; i++)
    {
        t = t->next ;
    }
    rt_kprintf("Current Elem is %d\n", t->elem);
    return t->elem;
}
/**
 * @function:delElem
 * @brief:删
 * @param {link} *p
 * @param {int} add
 * @return {*}
 * @author: lzc
 */
int delElem(link *p, int del_Data_Num)
{
    link *temp = p;
    link *del = NULL;
    int address, Num_pos_Delate = 0;
    int Count = 0;
    //遍历到被删除结点的上一个结点
    address = selectElem_FindElem_ReturnPos(p, del_Data_Num);
    if (address != -1)
    {
        for (int i = 1; i < address; i++)
        {
            temp = temp->next;
            if (temp->elem > del_Data_Num && temp->elem != DEFAULT_FF)
                temp->elem = temp->elem - 1;
        }
        del = temp->next;//单独设置一个指针指向被删除结点，以防丢失
        del->elem = DEFAULT_FF;
        Num_pos_Delate = del->Num_list;
        for (int i = 0; i < (RECORD_FAT_SIZE - address); i++)
        {
            del = del->next;
            if (del->elem > del_Data_Num && del->elem != DEFAULT_FF)
                del->elem = del->elem - 1;
        }
        temp = p;
        //只要temp指针指向的结点的next不是Null，就执行输出语句。
        while (temp->next)
        {
            temp = temp->next;
            Store_Num_Link_Info[Count++] = temp->elem ;
        }
        EEPROM_Write_StructData(RECORD_FAT_ADDR_START, Store_Num_Link_Info, sizeof(Store_Num_Link_Info));
    }
    display(p);
    return Num_pos_Delate;
}
/**
 * @function:delAllElem
 * @brief:全删
 * @param {link} *p
 * @param {int} add
 * @return {*}
 * @author: lzc
 */
link *delAllElem(link *p)
{
    link *temp = p;
    int Count = 0;
    //只要temp指针指向的结点的next不是Null，就执行输出语句。
    while (temp->next)
    {
        temp = temp->next;
        temp->elem = DEFAULT_FF;
        Store_Num_Link_Info[Count++] = temp->elem ;
    }
    EEPROM_Write_StructData(RECORD_FAT_ADDR_START, Store_Num_Link_Info, sizeof(Store_Num_Link_Info));
    return p;
}


/**
 * @function: insertElem
 * @brief:增
 * @param {link} p
 * @return {int}
 * @author: lzc
 */
int insertElem(link *p)
{
    link *temp = p; //创建临时结点temp
    int Count = 0;
    int Current_Max_elem = 0;
    int Num_pos_Insert = 0;
    char Reflash_Last_Record_Flag = 0;
    Check_History_Empty_Status(); // 查空
    Current_Max_elem = GetMax(p);
    // 此为空历史记录
    if (Empty_History_Flag == 1)
    {
        Current_Max_elem = 0;
        Empty_History_Flag = 0;
    }
    if (Current_Max_elem == RECORD_FAT_SIZE)
    {
        Reflash_Last_Record_Flag = 1;
        Current_Max_elem = RECORD_FAT_SIZE - 1;
        // 此处仅会更新最后一条记录,同时把第一条更新掉。
    }
    rt_kprintf("max  position %d\n", Current_Max_elem);
    temp = p;
    temp = temp->next; // 指向第一个节点
    //首先找到要插入位置
    for (int i = 1; i <= RECORD_FAT_SIZE; i++)
    {
        // rt_kprintf("Count %d Val %d\r\n", i, temp->elem);
        if (temp == NULL)
        {
            rt_kprintf("Invalid insert position\n");
            return -1;
        }
        // 更新空下来的
        if (temp->elem == DEFAULT_FF)
        {
            temp->elem = Current_Max_elem + 1;
            Num_pos_Insert = temp->Num_list;
            break;
        }
        // 更新最后一条,同时删除第一条
        if (Reflash_Last_Record_Flag)
        {
            i = 0; temp = p;
            temp = temp->next;  // 指向第一个节点
            delElem(p, 1);      // 删除第一条
            Reflash_Last_Record_Flag = 0;
            rt_kprintf("Reflash last position \n");
        }
        else
            temp = temp->next;
    }
    temp = p;
    //只要temp指针指向的结点的next不是Null，就执行输出语句。
    while (temp->next)
    {
        temp = temp->next;
        Store_Num_Link_Info[Count++] = temp->elem ;
    }
    EEPROM_Write_StructData(RECORD_FAT_ADDR_START, Store_Num_Link_Info, sizeof(Store_Num_Link_Info));
    display(p);
    return  Num_pos_Insert;
}


/**
 * @function:amendElem
 * @brief:改
 * @param {link} *p
 * @param {int} add
 * @param {int} newElem
 * @return {*}
 * @author: lzc
 */
link *amendElem(link *p, int add, int newElem)
{
    int i = 0;
    link *temp = p;
    temp = temp->next;//tamp指向首元结点
    //temp指向被删除结点
    for (i = 1; i < add; i++)
    {
        temp = temp->next;
    }
    temp->elem = newElem;
    return p;
}

link *History_Record_Ptr = NULL;
/**
 * @function: EEPROM_History_Init_FAT
 * @brief: 初始化存储结构体
 * @param {*}
 * @return {*}
 * @author: lzc
 */
void EEPROM_History_Init_FAT(void)
{
    rt_kprintf("Init Link:\n");
    History_Record_Ptr = initLink();
    display(History_Record_Ptr);
}

/**
 * @function: EEPROM_History_Add_One_Record
 * @brief: 增加一条新纪录
 * @param {*}
 * @return {*}
 * @author: lzc
 */
void EEPROM_History_Add_One_Record(void *Record_Write_Ptr, int Size)
{
    int Num_Record_Pos_Add = 0;
    rt_kprintf("Insert Record \n");
    if (History_Record_Ptr == NULL)
    {
        rt_kprintf("Please Init EEPROM First \n");
    }
    else
    {
        Num_Record_Pos_Add = insertElem(History_Record_Ptr);
        rt_kprintf("Insert Record Num Pos is %d\n", Num_Record_Pos_Add);
        // DONE: 此处增加对结构体的存储
        EEPROM_Write_StructData(RECORD_SAVE_ADDR_START + ((Num_Record_Pos_Add - 1) * Size),
                                Record_Write_Ptr, Size);
#if HISTORY_DEBUGB
        char str[10];
        sprintf(str, "%0.5f", Store_Test_Status_Write.Current_Test1);
        rt_kprintf("Store_Test_Status_Write.Current_Test1 is %s\r\n", str);
        sprintf(str, "%0.5f", Store_Test_Status_Write.Current_Test2);
        rt_kprintf("Store_Test_Status_Write.Current_Test2 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Write.Current_Test3);
        rt_kprintf("Store_Test_Status_Write.Current_Test3 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Write.Current_Test4);
        rt_kprintf("Store_Test_Status_Write.Current_Test4 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Write.Current_Test5);
        rt_kprintf("Store_Test_Status_Write.Current_Test5 is %s\r\n", str);
        sprintf(str, "%ld", Store_Test_Status_Write.Current_Test6);
        rt_kprintf("Store_Test_Status_Write.Current_Test6 is %s\r\n", str);

        // Test
        EEPROM_Read_StructData( RECORD_SAVE_ADDR_START + ((Num_Record_Pos_Add - 1) * Size),
                                &Store_Test_Status_Read, Size);
        sprintf(str, "%0.5f", Store_Test_Status_Read.Current_Test1);
        rt_kprintf("Store_Test_Status_Read.Current_Test1 is %s\r\n", str);
        sprintf(str, "%0.5f", Store_Test_Status_Read.Current_Test2);
        rt_kprintf("Store_Test_Status_Read.Current_Test2 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Read.Current_Test3);
        rt_kprintf("Store_Test_Status_Read.Current_Test3 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Read.Current_Test4);
        rt_kprintf("Store_Test_Status_Read.Current_Test4 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Read.Current_Test5);
        rt_kprintf("Store_Test_Status_Read.Current_Test5 is %s\r\n", str);
        sprintf(str, "%ld", Store_Test_Status_Read.Current_Test6);
        rt_kprintf("Store_Test_Status_Read.Current_Test6 is %s\r\n", str);
#endif
    }
}

/**
 * @function: EEPROM_History_Delate_One_Record
 * @brief: 删除一条新纪录
 * @param {uint16_t} Record_Num
 * @return {*}
 * @author: lzc
 */
void EEPROM_History_Delate_One_Record(uint16_t Record_Num, void *Record_Delate_Ptr, int Size)
{
    int Num_Record_Pos_Delate = 0;
    rt_kprintf("Delete Record Num %d \n", Record_Num);
    if (History_Record_Ptr == NULL)
    {
        rt_kprintf("Please Init EEPROM First \n");
    }
    else
    {
        Num_Record_Pos_Delate = delElem(History_Record_Ptr, Record_Num);
        if (Num_Record_Pos_Delate != 0)
            rt_kprintf("Delete_Record_Num Pos is %d \n", Num_Record_Pos_Delate);
        else
            rt_kprintf("Delete_Record Failed! No Such Record\n");
        // DONE: 此处增加对结构体的删除
        EEPROM_Write_StructData(RECORD_SAVE_ADDR_START + ((Num_Record_Pos_Delate - 1) * Size),
                                Record_Delate_Ptr, Size);
#if HISTORY_DEBUGB
        char str[10];
        sprintf(str, "%0.5f", Store_Test_Status_Delate.Current_Test1);
        rt_kprintf("Store_Test_Status_Delate.Current_Test1 is %s\r\n", str);
        sprintf(str, "%0.5f", Store_Test_Status_Delate.Current_Test2);
        rt_kprintf("Store_Test_Status_Delate.Current_Test2 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Delate.Current_Test3);
        rt_kprintf("Store_Test_Status_Delate.Current_Test3 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Delate.Current_Test4);
        rt_kprintf("Store_Test_Status_Delate.Current_Test4 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Delate.Current_Test5);
        rt_kprintf("Store_Test_Status_Delate.Current_Test5 is %s\r\n", str);
        sprintf(str, "%ld", Store_Test_Status_Delate.Current_Test6);
        rt_kprintf("Store_Test_Status_Delate.Current_Test6 is %s\r\n", str);

        // Test
        EEPROM_Read_StructData( RECORD_SAVE_ADDR_START + ((Num_Record_Pos_Delate - 1) * Size),
                                &Store_Test_Status_Read, Size);
        sprintf(str, "%0.5f", Store_Test_Status_Read.Current_Test1);
        rt_kprintf("Store_Test_Status_Read.Current_Test1 is %s\r\n", str);
        sprintf(str, "%0.5f", Store_Test_Status_Read.Current_Test2);
        rt_kprintf("Store_Test_Status_Read.Current_Test2 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Read.Current_Test3);
        rt_kprintf("Store_Test_Status_Read.Current_Test3 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Read.Current_Test4);
        rt_kprintf("Store_Test_Status_Read.Current_Test4 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Read.Current_Test5);
        rt_kprintf("Store_Test_Status_Read.Current_Test5 is %s\r\n", str);
        sprintf(str, "%ld", Store_Test_Status_Read.Current_Test6);
        rt_kprintf("Store_Test_Status_Read.Current_Test6 is %s\r\n", str);
#endif
    }
}
/**
 * @function: EEPROM_History_Select_One_Record
 * @brief: 查一条新纪录
 * @param {uint16_t} Record_Num
 * @return {*}
 * @author: lzc
 */
void EEPROM_History_Select_One_Record(uint16_t Record_Num, void *Record_Read_Ptr, int Size)
{
    void *Read_Ptr = Record_Read_Ptr;
    int Num_Record_Pos_Select = 0;
    rt_kprintf("Select Record Num %d \n", Record_Num);
    if (History_Record_Ptr == NULL)
    {
        rt_kprintf("Please Init EEPROM First \n");
    }
    else
    {
        Num_Record_Pos_Select = selectElem_FindElem_ReturnPos(History_Record_Ptr, Record_Num);
        if (Num_Record_Pos_Select != 0)
            rt_kprintf("Num_Record_Pos_Select Pos is %d \n", Num_Record_Pos_Select);
        else
            rt_kprintf("Num_Record_Pos_Select Failed! No Such Record\n");
        // DONE: 此处增加对结构体的打印或者显示
        EEPROM_Read_StructData( RECORD_SAVE_ADDR_START + ((Num_Record_Pos_Select - 1) * Size),
                                Read_Ptr, Size);
        Record_Read_Ptr = Read_Ptr;
#if HISTORY_DEBUGB
        char str[10];
        sprintf(str, "%0.5f", Store_Test_Status_Read.Current_Test1);
        rt_kprintf("Store_Test_Status_Read.Current_Test1 is %s\r\n", str);
        sprintf(str, "%0.5f", Store_Test_Status_Read.Current_Test2);
        rt_kprintf("Store_Test_Status_Read.Current_Test2 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Read.Current_Test3);
        rt_kprintf("Store_Test_Status_Read.Current_Test3 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Read.Current_Test4);
        rt_kprintf("Store_Test_Status_Read.Current_Test4 is %s\r\n", str);
        sprintf(str, "%d", Store_Test_Status_Read.Current_Test5);
        rt_kprintf("Store_Test_Status_Read.Current_Test5 is %s\r\n", str);
        sprintf(str, "%ld", Store_Test_Status_Read.Current_Test6);
        rt_kprintf("Store_Test_Status_Read.Current_Test6 is %s\r\n", str);
#endif
    }
}
/**
 * @function: EEPROM_History_Delate_All_Record
 * @brief: 删除全部记录
 * @param {*}
 * @return {*}
 * @author: lzc
 */
void EEPROM_History_Delate_All_Record(void)
{
    rt_kprintf("Delete All\n");
    if (History_Record_Ptr == NULL)
    {
        rt_kprintf("Please Init EEPROM First \n");
    }
    else
    {
        History_Record_Ptr = delAllElem(History_Record_Ptr);
        display(History_Record_Ptr);
        Empty_History_Flag = 1;
    }
}
/**
 * @function:
 * @brief:
 * @param {int} argc
 * @param {char} *argv
 * @return {*}
 * @author: lzc
 */
void History(int argc, char *argv[])
{
    rt_kprintf("argc is %d\n", argc);
    if (argc < 2)
    {
        rt_kprintf("|    History Record Input Table |\r\n");
        rt_kprintf("|      * History Insert         |\r\n");
        rt_kprintf("|      * History Select <Num>   |\r\n");
        rt_kprintf("|      * History Delate <Num>   |\r\n");
        rt_kprintf("|      * History Delate All     |\r\n");
        rt_kprintf("|      * History Init           |\r\n");
    }
    else if (argc < 3)
    {
        if (!strcmp(argv[1], "Insert"))
        {
            Store_Test_Status_Write.Current_Test1 = 0.07485;
            Store_Test_Status_Write.Current_Test2 = -0.07485;
            Store_Test_Status_Write.Current_Test3 = 34512;
            Store_Test_Status_Write.Current_Test4 = 122;
            Store_Test_Status_Write.Current_Test5 = true;
            Store_Test_Status_Write.Current_Test6 = 1312334444;
            EEPROM_History_Add_One_Record(  &Store_Test_Status_Write,
                                            sizeof(Store_Test_Status_Write));
        }
        else if (!strcmp(argv[1], "Init"))
        {
            EEPROM_History_Init_FAT();
        }
    }
    else if (argc < 4)
    {
        if (!strcmp(argv[1], "Delate"))
        {
            if (!strcmp(argv[2], "All"))
            {
                EEPROM_History_Delate_All_Record();
            }
            else
            {
                Store_Test_Status_Delate.Current_Test1 = 1 ;
                Store_Test_Status_Delate.Current_Test2 = 1 ;
                Store_Test_Status_Delate.Current_Test3 = 1 ;
                Store_Test_Status_Delate.Current_Test4 = 1 ;
                Store_Test_Status_Delate.Current_Test5 = 1 ;
                Store_Test_Status_Delate.Current_Test6 = 1 ;
                rt_kprintf("Value of Delate %d\r\n", atoi(argv[2]));
                EEPROM_History_Delate_One_Record(   atoi(argv[2]),
                                                    &Store_Test_Status_Delate,
                                                    sizeof(Store_Test_Status_Delate));
            }
        }
        else if (!strcmp(argv[1], "Select"))
        {
            Store_Test_Status_Read.Current_Test1 = 0 ;
            Store_Test_Status_Read.Current_Test2 = 0 ;
            Store_Test_Status_Read.Current_Test3 = 0 ;
            Store_Test_Status_Read.Current_Test4 = 0 ;
            Store_Test_Status_Read.Current_Test5 = 0 ;
            Store_Test_Status_Read.Current_Test6 = 0 ;
            rt_kprintf("Value of Select %d\r\n", atoi(argv[2]));
            EEPROM_History_Select_One_Record(   atoi(argv[2]),
                                                &Store_Test_Status_Read,
                                                sizeof(Store_Test_Status_Read));
        }
    }
}
MSH_CMD_EXPORT(History, History function test);


/**
 * @function:
 * @brief:
 * @param {*}
 * @return {*}
 * @author: lzc
 */
void Link_Test(int argc, char *argv[])
{
    link *p = NULL;
    int address;
    int Value;

    rt_kprintf("Init Link:\n");
    p = initLink();
    display(p);

    rt_kprintf("Insert element \n");
    insertElem(p);

    rt_kprintf("Delete element 7 \n");
    delElem(p, 7);

    rt_kprintf("Find element 2 at:\n");
    address = selectElem_FindElem_ReturnPos(p, 2);
    if (address == -1)
    {
        rt_kprintf("There is no element");
    }
    else
    {
        rt_kprintf("The position of element 2 is:%d\n", address);
    }

    Value = selectElem_FindPosition(p, 5);
    rt_kprintf("The position 5 of element is:%d\n", Value);
}
MSH_CMD_EXPORT(Link_Test, Link function test);

/**
 * @function: EEPROM_Test
 * @brief: 测试
 * @param {int} argc
 * @param {char} *argv
 * @return {*}
 * @author: lzc
 */
void EEPROM_Test(int argc, char *argv[])
{
    char str[10];
    static Store_Test_Status_t Store_Test_Status_2;
    Store_Test_Status.Current_Test1 = 0.07485;
    Store_Test_Status.Current_Test2 = -0.07485;
    Store_Test_Status.Current_Test3 = 34512;
    Store_Test_Status.Current_Test4 = 213;
    Store_Test_Status.Current_Test5 = true;
    Store_Test_Status.Current_Test6 = 1312333213;

    EEPROM_Write_StructData(0, &Store_Test_Status, sizeof(Store_Test_Status));
    EEPROM_Read_StructData(0, &Store_Test_Status_2, sizeof(Store_Test_Status_2));

    sprintf(str, "%0.5f", Store_Test_Status.Current_Test1);
    rt_kprintf("Store_Test_Status.Current_Test1 is %s\r\n", str);
    sprintf(str, "%0.5f", Store_Test_Status.Current_Test2);
    rt_kprintf("Store_Test_Status.Current_Test2 is %s\r\n", str);
    sprintf(str, "%d", Store_Test_Status.Current_Test3);
    rt_kprintf("Store_Test_Status.Current_Test3 is %s\r\n", str);
    sprintf(str, "%d", Store_Test_Status.Current_Test4);
    rt_kprintf("Store_Test_Status.Current_Test4 is %s\r\n", str);
    sprintf(str, "%d", Store_Test_Status.Current_Test5);
    rt_kprintf("Store_Test_Status.Current_Test5 is %s\r\n", str);
    sprintf(str, "%ld", Store_Test_Status.Current_Test6);
    rt_kprintf("Store_Test_Status.Current_Test6 is %s\r\n", str);

    rt_kprintf("\r\n");

    sprintf(str, "%0.5f", Store_Test_Status_2.Current_Test1);
    rt_kprintf("Store_Test_Status_2.Current_Test1 is %s\r\n", str);
    sprintf(str, "%0.5f", Store_Test_Status_2.Current_Test2);
    rt_kprintf("Store_Test_Status_2.Current_Test2 is %s\r\n", str);
    sprintf(str, "%d", Store_Test_Status_2.Current_Test3);
    rt_kprintf("Store_Test_Status_2.Current_Test3 is %s\r\n", str);
    sprintf(str, "%d", Store_Test_Status_2.Current_Test4);
    rt_kprintf("Store_Test_Status_2.Current_Test4 is %s\r\n", str);
    sprintf(str, "%d", Store_Test_Status_2.Current_Test5);
    rt_kprintf("Store_Test_Status_2.Current_Test5 is %s\r\n", str);
    sprintf(str, "%ld", Store_Test_Status_2.Current_Test6);
    rt_kprintf("Store_Test_Status_2.Current_Test6 is %s\r\n", str);
    EEPROM_Write_StructData(RECORD_FAT_ADDR_START, Test_Num_Link, sizeof(Test_Num_Link));
}
MSH_CMD_EXPORT(EEPROM_Test, eeprom function test);
